package com.fw.ui.view.settings.passwordpolicy.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.ErrorBean;
import com.fw.service.passwordpolicy.bean.PasswordPolicyBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.ConfigurationBAO;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.passwordpolicy.EditPasswordPolicyView;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditPasswordPolicySaveButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private ConfigurationBAO configurationBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private UICommonUtils uiCommonUtils;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	EditPasswordPolicyView editPasswordPolicyView = (EditPasswordPolicyView) this.getBasedComponent();

	String minPasswordLength = editPasswordPolicyView.getTxtMinPasswordLength().getValue();
	String enforePasswordHistory = editPasswordPolicyView.getTxtEnforePasswordHistory().getValue();
	String minUpperCase = editPasswordPolicyView.getTxtMinUpperCase().getValue();
	String minNumeric = editPasswordPolicyView.getTxtMinNumeric().getValue();
	String minSpecialCharacter = editPasswordPolicyView.getTxtMinSpecialCharacter().getValue();
	String passwordExpiryPeriodDay = editPasswordPolicyView.getTxtPasswordExpiryPeriodDay().getValue();
	String adminLoingExpirationDay = editPasswordPolicyView.getTxtAdminLoingExpirationDay().getValue();
	String merchantLoingExpirationDay = editPasswordPolicyView.getTxtMerchantLoingExpirationDay().getValue();
	String loginAttemptAllowed = editPasswordPolicyView.getTxtLoginAttemptAllowed().getValue();
	String passwordNotificationExpireDay = editPasswordPolicyView.getTxtPasswordNotificationExpireDay().getValue();

	// PasswordPolicyBean oldPasswordPolicyBean = editPasswordPolicyView.getPasswordPolicyBean();

	PasswordPolicyBean passwordPolicyBean = new PasswordPolicyBean();
	passwordPolicyBean.setMinPasswordLength(minPasswordLength != null && minPasswordLength.length() > 0 ? Integer
		.parseInt(minPasswordLength) : 0);
	passwordPolicyBean.setEnforePasswordHistory(enforePasswordHistory != null && enforePasswordHistory.length() > 0 ? Integer
		.parseInt(enforePasswordHistory) : 0);
	passwordPolicyBean.setMinUpperCase(minUpperCase != null && minUpperCase.length() > 0 ? Integer.parseInt(minUpperCase) : 0);
	passwordPolicyBean.setMinNumeric(minNumeric != null && minNumeric.length() > 0 ? Integer.parseInt(minNumeric) : 0);
	passwordPolicyBean.setMinSpecialCharacter(minSpecialCharacter != null && minSpecialCharacter.length() > 0 ? Integer
		.parseInt(minSpecialCharacter) : 0);
	passwordPolicyBean.setPasswordExpiryPeriodDay(passwordExpiryPeriodDay != null && passwordExpiryPeriodDay.length() > 0 ? Integer
		.parseInt(passwordExpiryPeriodDay) : 0);
	passwordPolicyBean.setAdminLoingExpirationDay(adminLoingExpirationDay != null && adminLoingExpirationDay.length() > 0 ? Integer
		.parseInt(adminLoingExpirationDay) : 0);
	passwordPolicyBean
		.setMerchantLoingExpirationDay(merchantLoingExpirationDay != null && merchantLoingExpirationDay.length() > 0 ? Integer
			.parseInt(merchantLoingExpirationDay) : 0);
	passwordPolicyBean.setLoginAttemptAllowed(loginAttemptAllowed != null && loginAttemptAllowed.length() > 0 ? Integer
		.parseInt(loginAttemptAllowed) : 0);
	passwordPolicyBean.setPasswordNotificationExpireDay(passwordNotificationExpireDay != null
		&& passwordNotificationExpireDay.length() > 0 ? Integer.parseInt(passwordNotificationExpireDay) : 0);

	ApplicationUI applicationUI = (ApplicationUI) editPasswordPolicyView.getUI();
	sessionInfoUtils.setSessionInfoDetails(passwordPolicyBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	Map<String, Serializable> mapResult = this.configurationBAO.updatePasswordPolicy(passwordPolicyBean);
	Map<String, Object> result = new HashMap<String, Object>();
	if (mapResult != null) {
	    List<ErrorBean> errorBeans = (List<ErrorBean>) mapResult.get(List.class.getName() + "." + ErrorBean.class.getName());
	    passwordPolicyBean = (PasswordPolicyBean) mapResult.get(PasswordPolicyBean.class.getName());
	    if (errorBeans != null && !errorBeans.isEmpty()) {
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, errorBeans);
	    } else {
		if (passwordPolicyBean != null) {
		    editPasswordPolicyView.setPasswordPolicyBean(passwordPolicyBean);

		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtMinPasswordLength(),
			    passwordPolicyBean.getMinPasswordLength() == null ? "" : passwordPolicyBean.getMinPasswordLength().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtEnforePasswordHistory(), passwordPolicyBean
			    .getEnforePasswordHistory() == null ? "" : passwordPolicyBean.getEnforePasswordHistory().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtMinUpperCase(),
			    passwordPolicyBean.getMinUpperCase() == null ? "" : passwordPolicyBean.getMinUpperCase().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtMinNumeric(),
			    passwordPolicyBean.getMinNumeric() == null ? "" : passwordPolicyBean.getMinNumeric().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtMinSpecialCharacter(), passwordPolicyBean
			    .getMinSpecialCharacter() == null ? "" : passwordPolicyBean.getMinSpecialCharacter().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtPasswordExpiryPeriodDay(), passwordPolicyBean
			    .getPasswordExpiryPeriodDay() == null ? "" : passwordPolicyBean.getPasswordExpiryPeriodDay().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtAdminLoingExpirationDay(), passwordPolicyBean
			    .getAdminLoingExpirationDay() == null ? "" : passwordPolicyBean.getAdminLoingExpirationDay().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtMerchantLoingExpirationDay(), passwordPolicyBean
			    .getMerchantLoingExpirationDay() == null ? "" : passwordPolicyBean.getMerchantLoingExpirationDay().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtLoginAttemptAllowed(), passwordPolicyBean
			    .getLoginAttemptAllowed() == null ? "" : passwordPolicyBean.getLoginAttemptAllowed().toString());
		    uiCommonUtils.setValueUIComponent(editPasswordPolicyView.getTxtPasswordNotificationExpireDay(), passwordPolicyBean
			    .getPasswordNotificationExpireDay() == null ? "" : passwordPolicyBean.getPasswordNotificationExpireDay()
			    .toString());

		    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(editPasswordPolicyView);

		    result.put(MessageBAO.MESSAGE_INFO_TYPE,
			    "com.fw.ui.view.settings.passwordpolicy.EditPasswordPolicyView.message.updated");
		} else {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE,
			    "com.fw.ui.view.settings.passwordpolicy.EditPasswordPolicyView.message.failed");
		}
	    }
	} else {
	    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "message.error.common");
	}
	return result;
    }

    @Override
    public boolean validate(ClickEvent event) {
	EditPasswordPolicyView editPasswordPolicyView = (EditPasswordPolicyView) this.getBasedComponent();
	try {

	    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(editPasswordPolicyView);

	    boolean minPasswordLengthValid = editPasswordPolicyView.getTxtMinPasswordLength().isValid();
	    boolean enforePasswordHistoryValid = editPasswordPolicyView.getTxtEnforePasswordHistory().isValid();
	    boolean minUpperCaseValid = editPasswordPolicyView.getTxtMinUpperCase().isValid();
	    boolean minNumericValid = editPasswordPolicyView.getTxtMinNumeric().isValid();
	    boolean minSpecialCharacterValid = editPasswordPolicyView.getTxtMinSpecialCharacter().isValid();
	    boolean passwordExpiryPeriodDayValid = editPasswordPolicyView.getTxtPasswordExpiryPeriodDay().isValid();
	    boolean adminLoingExpirationDayValid = editPasswordPolicyView.getTxtAdminLoingExpirationDay().isValid();
	    boolean merchantLoingExpirationDayValid = editPasswordPolicyView.getTxtMerchantLoingExpirationDay().isValid();
	    boolean loginAttemptAllowedValid = editPasswordPolicyView.getTxtLoginAttemptAllowed().isValid();
	    boolean passwordNotificationExpireDayValid = editPasswordPolicyView.getTxtPasswordNotificationExpireDay().isValid();

	    if (minPasswordLengthValid && enforePasswordHistoryValid && minUpperCaseValid && minNumericValid && minSpecialCharacterValid
		    && passwordExpiryPeriodDayValid && adminLoingExpirationDayValid && merchantLoingExpirationDayValid
		    && loginAttemptAllowedValid && passwordNotificationExpireDayValid) {
		return true;
	    } else {
		if (!minPasswordLengthValid) {
		    editPasswordPolicyView.getTxtMinPasswordLength().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtMinPasswordLength().getErrorMessage() != null) {
			editPasswordPolicyView.getLblMinPasswordLengthValidateMessage().setValue(
				editPasswordPolicyView.getTxtMinPasswordLength().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow1Validate().setVisible(true);
		}

		if (!enforePasswordHistoryValid) {
		    editPasswordPolicyView.getTxtEnforePasswordHistory().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtEnforePasswordHistory().getErrorMessage() != null) {
			editPasswordPolicyView.getLblEnforePasswordHistoryValidateMessage().setValue(
				editPasswordPolicyView.getTxtEnforePasswordHistory().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow2Validate().setVisible(true);
		}

		if (!minUpperCaseValid) {
		    editPasswordPolicyView.getTxtMinUpperCase().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtMinUpperCase().getErrorMessage() != null) {
			editPasswordPolicyView.getLblMinUpperCaseValidateMessage().setValue(
				editPasswordPolicyView.getTxtMinUpperCase().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow3Validate().setVisible(true);
		}

		if (!minNumericValid) {
		    editPasswordPolicyView.getTxtMinNumeric().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtMinNumeric().getErrorMessage() != null) {
			editPasswordPolicyView.getLblMinNumericValidateMessage().setValue(
				editPasswordPolicyView.getTxtMinNumeric().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow4Validate().setVisible(true);
		}

		if (!minSpecialCharacterValid) {
		    editPasswordPolicyView.getTxtMinSpecialCharacter().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtMinSpecialCharacter().getErrorMessage() != null) {
			editPasswordPolicyView.getLblMinSpecialCharacterValidateMessage().setValue(
				editPasswordPolicyView.getTxtMinSpecialCharacter().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow5Validate().setVisible(true);
		}

		if (!passwordExpiryPeriodDayValid) {
		    editPasswordPolicyView.getTxtPasswordExpiryPeriodDay().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtPasswordExpiryPeriodDay().getErrorMessage() != null) {
			editPasswordPolicyView.getLblPasswordExpiryPeriodDayValidateMessage().setValue(
				editPasswordPolicyView.getTxtPasswordExpiryPeriodDay().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow6Validate().setVisible(true);
		}

		if (!adminLoingExpirationDayValid) {
		    editPasswordPolicyView.getTxtAdminLoingExpirationDay().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtAdminLoingExpirationDay().getErrorMessage() != null) {
			editPasswordPolicyView.getLblAdminLoingExpirationDayValidateMessage().setValue(
				editPasswordPolicyView.getTxtAdminLoingExpirationDay().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow7Validate().setVisible(true);
		}

		if (!merchantLoingExpirationDayValid) {
		    editPasswordPolicyView.getTxtMerchantLoingExpirationDay().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtMerchantLoingExpirationDay().getErrorMessage() != null) {
			editPasswordPolicyView.getLblMerchantLoingExpirationDayValidateMessage().setValue(
				editPasswordPolicyView.getTxtMerchantLoingExpirationDay().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow8Validate().setVisible(true);
		}

		if (!loginAttemptAllowedValid) {
		    editPasswordPolicyView.getTxtLoginAttemptAllowed().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtLoginAttemptAllowed().getErrorMessage() != null) {
			editPasswordPolicyView.getLblLoginAttemptAllowedValidateMessage().setValue(
				editPasswordPolicyView.getTxtLoginAttemptAllowed().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow9Validate().setVisible(true);
		}

		if (!passwordNotificationExpireDayValid) {
		    editPasswordPolicyView.getTxtPasswordNotificationExpireDay().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtPasswordNotificationExpireDay().getErrorMessage() != null) {
			editPasswordPolicyView.getLblPasswordNotificationExpireDayValidateMessage().setValue(
				editPasswordPolicyView.getTxtPasswordNotificationExpireDay().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow10Validate().setVisible(true);
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
	return false;
    }

}
